var t = require("./configs.js");

App({
    onLaunch: function() {
        var t = this;
        wx.getSystemInfo({
            success: function(o) {
                t.globalData.isIpx = !(o.model.indexOf("iPhone X") < 0), t.globalData.systemInfo = o;
            },
            fail: function(t) {},
            complete: function(t) {}
        }), this.checkUpdate();
    },
    checkUpdate: function() {
        if (wx.getUpdateManager) {
            var t = wx.getUpdateManager();
            t.onCheckForUpdate(function(t) {
                console.log(t.hasUpdate);
            }), t.onUpdateReady(function() {
                wx.showModal({
                    title: "更新提示",
                    content: "新版本已经准备好，是否重启应用？",
                    success: function(o) {
                        o.confirm && t.applyUpdate();
                    }
                });
            }), t.onUpdateFailed(function() {});
        } else console.error("The SDK is too old to support wx.getUpdateManager()");
    },
    onHide: function() {},
    onShow: function() {
        var t = wx.getStorageSync("is_test_env");
        console.info("app.onLaunch: isTestEnv", t), t && (this.globalData.BASE_URL = "https://nitro-api-test.nicokids.cn/v1"), 
        this.globalData.isTestEnv = !!t, this.globalData.BASE_URL.indexOf("nitro-api-test") > -1 && wx.showToast({
            title: "当前正在使用测试环境",
            icon: "none",
            duration: 2e3
        });
    },
    globalData: {
        userInfo: null,
        code: "",
        userId: "",
        token: "",
        cart: {
            storeId: null,
            store: null,
            inventory: null,
            shipping_address: {}
        },
        tenantId: "5a747495ce615177009f6400",
        appId: t.appId,
        BASE_URL: "https://nitro-api.nicokids.cn/v1",
        stores: [],
        isIpx: !1,
        systemInfo: {}
    },
    markAsDebug: function(t) {
        var o = !!t;
        o ? (this.globalData.BASE_URL = "https://nitro-api-test.nicokids.cn/v1", this.globalData.isTestEnv = !0, 
        wx.setStorageSync("is_test_env", o), wx.removeStorageSync("token"), wx.removeStorageSync("userId"), 
        wx.removeStorageSync("userInfo")) : (wx.clearStorageSync(), this.globalData.BASE_URL = "https://nitro-api.nicokids.cn/v1", 
        this.globalData.isTestEnv = !1), this.globalData.token = "", wx.showModal({
            title: "已切换至**" + (o ? "测试" : "生产") + "**环境",
            content: "需重启微信，再次登录后才能生效",
            showCancel: !1,
            confirmText: "知道了",
            success: function() {
                wx.reLaunch({
                    url: "/pages/stores/stores"
                });
            }
        });
    },
    login: function(t) {
        console.info("app.login()");
        var o = this;
        wx.login({
            success: function(e) {
                var n = e.code;
                console.info("wx.login() code >", n), o.globalData.code = n;
                try {
                    var a = wx.getStorageSync("token"), i = wx.getStorageSync("userId"), s = wx.getStorageSync("userInfo");
                    console.log("check token", a), console.log("check userId", i), console.log("check userInfo", s), 
                    a && i ? (o.globalData.token = a, o.globalData.userId = i, o.refreshUserProfile(function() {
                        "function" == typeof t && t(e);
                    })) : (wx.removeStorageSync("token"), wx.removeStorageSync("userId")
                    //  ,o.navigateLogin()
                     );
                } catch (t) {
                    // o.navigateLogin();
                }
            },
            fail: function(t) {
                console.info("app.login() failed", t);
            },
            complete: function(t) {
                console.info("app.login() complete", t);
            }
        });
    },
    refreshUserProfile: function(t) {
        var o = this;
        this.getRequest(this.globalData.BASE_URL + "/auth/profile", {}).then(function(e) {
            console.log("userInfo", e), o.globalData.userInfo = e, wx.setStorageSync("userInfo", e), 
            t && "function" == typeof t && t();
        });
    },
    navigateLogin: function() {
        wx.redirectTo({
            url: "/pages/login/login",
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    showErrorMsg: function(t, o) {
        wx.showModal({
            title: t,
            content: o || "",
            showCancel: !1,
            confirmText: "知道了"
        });
    },
    getRequest: function(o, e) {
        var n = this;
        return new Promise(function(a, i) {
            wx.showNavigationBarLoading(), wx.request({
                url: o,
                data: e,
                method: "GET",
                header: {
                    "X-Tenant-Id": n.globalData.tenantId,
                    "X-API-Token": n.globalData.token,
                    "X-MP-Id": t.appId
                },
                success: function(t) {
                    401 == t.statusCode ? (wx.removeStorageSync("token"), wx.removeStorageSync("userId"), 
                    n.globalData.token = "", wx.reLaunch({
                        url: "/pages/stores/stores"
                    })) : 200 == t.statusCode || 204 == t.statusCode ? a(t.data) : (console.error(t), 
                    console.error("error", t));
                },
                fail: function(t) {},
                complete: function(t) {
                    function o(o) {
                        return t.apply(this, arguments);
                    }
                    return o.toString = function() {
                        return t.toString();
                    }, o;
                }(function(t) {
                    "function" == typeof complete && complete(t), wx.hideNavigationBarLoading();
                })
            });
        });
    },
    postRequest: function(o, e) {
        var n = this;
        return new Promise(function(a, i) {
            wx.showNavigationBarLoading(), wx.request({
                url: o,
                data: e,
                method: "POST",
                header: {
                    "X-Tenant-Id": n.globalData.tenantId,
                    "X-API-Token": n.globalData.token,
                    "X-MP-Id": t.appId
                },
                success: function(t) {
                    401 == t.statusCode ? (n.globalData.token = "", wx.reLaunch({
                        url: "/pages/stores/stores"
                    })) : 200 == t.statusCode ? a(t.data) : (console.error("error", t), i(t.data));
                },
                fail: function(t) {},
                complete: function(t) {
                    function o(o) {
                        return t.apply(this, arguments);
                    }
                    return o.toString = function() {
                        return t.toString();
                    }, o;
                }(function(t) {
                    "function" == typeof complete && complete(t), wx.hideNavigationBarLoading();
                })
            });
        });
    },
    patchRequest: function(o, e) {
        var n = this;
        return new Promise(function(a, i) {
            wx.showNavigationBarLoading(), wx.request({
                url: o,
                data: e,
                method: "PUT",
                header: {
                    "X-Tenant-Id": n.globalData.tenantId,
                    "X-API-Token": n.globalData.token,
                    "X-MP-Id": t.appId
                },
                success: function(t) {
                    401 == t.statusCode ? (n.globalData.token = "", wx.reLaunch({
                        url: "/pages/stores/stores"
                    })) : 200 == t.statusCode ? a(t.data) : (console.error("error", t), i(t.data));
                },
                fail: function(t) {
                    console.error("fail", t);
                },
                complete: function(t) {
                    function o(o) {
                        return t.apply(this, arguments);
                    }
                    return o.toString = function() {
                        return t.toString();
                    }, o;
                }(function(t) {
                    "function" == typeof complete && complete(t), wx.hideNavigationBarLoading();
                })
            });
        });
    },
    campaignEventLog: function(t) {
        return;
    }
});